﻿#include "financekeepstandarditemmodel.h"
#include <QDebug>

FinanceKeepStandardItemModel::FinanceKeepStandardItemModel(QObject *parent) : QStandardItemModel(parent)
{
    header<<"类型"
         <<"一级类别"
        <<"二级类别"
       <<"科目";

    setHorizontalHeaderLabels(header);
}

void FinanceKeepStandardItemModel::insertFinanceItem(FinanceItem &financeItem){
    QList<QStandardItem*> rowData;

    rowData.append(new QStandardItem("财务记账"));
    rowData.append(new QStandardItem(financeItem.type==0?"收入":"支出"));
    rowData.append(new QStandardItem(financeItem.category));
    rowData.append(new QStandardItem(financeItem.subject));

    appendRow(rowData);
    financeItems.append(&financeItem);
}

void FinanceKeepStandardItemModel::insertFinanceItem(
        QList<FinanceItem*> financeItems,
        QString primaryDirectory,
        QString secondaryDirectory){

    for(FinanceItem *financeItem:financeItems){
        if(primaryDirectory.isEmpty()){
            insertFinanceItem(*financeItem);
        }else if(primaryDirectory == "收入" && financeItem->type == 0){
            //收入
            if(secondaryDirectory.isEmpty() ||
                    secondaryDirectory == "所有收入" ||
                    secondaryDirectory == financeItem->category){
                insertFinanceItem(*financeItem);
            }
        }else if(primaryDirectory == "支出" && financeItem->type == 1){
            //支出
            if(secondaryDirectory.isEmpty() ||
                    secondaryDirectory == "所有支出" ||
                    secondaryDirectory == financeItem->category){
                insertFinanceItem(*financeItem);
            }
        }
    }
}

void FinanceKeepStandardItemModel::reset(){
    clear();
    financeItems.clear();
    setHorizontalHeaderLabels(header);
}

FinanceItem* FinanceKeepStandardItemModel::getFinanceItem(int index){
    if(index <0 || index>=financeItems.size()){
        return nullptr;
    }

    return financeItems[index];
}
